package cn.zwx.common.canal.listener;

import cn.zwx.common.canal.bo.CovertMessageBO;
import cn.zwx.common.canal.enums.BuisType;
import cn.zwx.common.canal.event.DeleteAbstractCanalEvent;
import cn.zwx.common.canal.handler.CovertMessageHandler;
import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class DeleteCanalListener extends AbstractCanalListener<DeleteAbstractCanalEvent>{

    private final Logger logger = LoggerFactory.getLogger(DeleteCanalListener.class);
    @Autowired
    private CovertMessageHandler covertMessageHandler;

    @Override
    protected void doChange(String database, String tableName, CanalEntry.RowData rowData,String instanceName,String topic) {
        logger.info("监听到删除事件:数据库名称{},表名称:{},实例:{},topic:{}",database,tableName,instanceName,topic);
        // 删除之前的数据
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        // 转换发送消息类
        CovertMessageBO messageBO = new CovertMessageBO();
        messageBO.setDatabase(database);
        messageBO.setBusiType(BuisType.DELETE.getCode());
        messageBO.setTableName(tableName);
        messageBO.setBeforeColumnsList(beforeColumnsList);
        Map<String, Object> messageMap = covertMessageHandler.covertMessageDTO(messageBO);
        // 发送MQ
        logger.info(String.format("{%s},发送mq请求参数:",BuisType.DELETE.getMsg())+ JSON.toJSONString(messageMap));

    }
}
